<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="填报日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.date`" :rules="formRules.date" class="mb-0px!">
            <el-date-picker
              v-model="row.date"
              type="date"
              value-format="x"
              placeholder="选择填报日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="资料类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.dataType`" :rules="formRules.dataType" class="mb-0px!">
            <el-select v-model="row.dataType" placeholder="请选择资料类型">
                <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.CRM_PROJECT_ACCEPTANCE_TYPE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="附件" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.fileUrls`" :rules="formRules.fileUrls" class="mb-0px!">
            <UploadFile v-model="row.fileUrls" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加CRM 企业项目信息-项目验收资料</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ProjectApi } from '@/api/crm/project'

const props = defineProps<{
  projectId: undefined // 客户项目信息id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  projectId: [{ required: true, message: '客户项目信息id不能为空', trigger: 'blur' }],
  date: [{ required: true, message: '填报日期不能为空', trigger: 'blur' }],
  dataType: [{ required: true, message: '资料类型不能为空', trigger: 'change' }],
  fileUrls: [{ required: true, message: '附件不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.projectId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await ProjectApi.getProjectCheckListByProjectId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    projectId: undefined,
    date: undefined,
    dataType: undefined,
    fileUrls: undefined,
    remark: undefined,
  }
  row.projectId = props.projectId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>